package com.zzty.training.controller;

import com.zzty.training.entity.Question;
import com.zzty.training.entity.SelfExam;
import com.zzty.training.entity.TeamExam;
import com.zzty.training.service.QuestionService;
import com.zzty.training.service.TeamExamService;
import com.zzty.training.service.SelfExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    QuestionService questionService;
    @Autowired
    TeamExamService teamExamService;
    @Autowired
    SelfExamService selfExamService;

    @RequestMapping(value = "/getAllQuestion")
    public List<Question> getAllQuestion() {
        List<Question> questions = questionService.getAllQuestion();
        return questions;
    }

    @RequestMapping(value = "/addQuestion")
    public Question addQuestion(Question question) {
        questionService.addQuestion(question);
        Question question1 = questionService.getQuestionByContent(question);
        return question1;
    }

    @RequestMapping(value = "/teamExam/checkDate/{date}")
    public String checkDate(@PathVariable("date") String date) {
        String result = "false";
        List<TeamExam> teamExams = teamExamService.getAllTeamExam();
        for (TeamExam teamExam : teamExams) {
            if (teamExam.getDate().equals(date)) {
                result = "true";
                break;
            }
        }
        return result;
    }

    @RequestMapping(value = "/addTeamExam")
    public Integer addTeamExam(TeamExam teamExam) {
        System.out.println(teamExam);
        Integer result = teamExamService.addTeamExam(teamExam);

        return result;
    }
    @RequestMapping(value = "/selfExam/checkDate/{date}")
    public String checkDate1(@PathVariable("date") String date) {
        String result = "false";
        List<SelfExam> selfExams = selfExamService.getAllSelfExam();
        for (SelfExam selfExam : selfExams) {
            if (selfExam.getDate().equals(date)) {
                result = "true";
                break;
            }
        }
        return result;
    }

    @RequestMapping(value = "/addSelfExam")
    public Integer addTeamExam(SelfExam selfExam) {
        System.out.println(selfExam);
        Integer result = selfExamService.addSelfExam(selfExam);

        return result;
    }
}
